package com.xk857.simpleeffect.activity;

import android.app.DatePickerDialog;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.reflect.TypeToken;
import com.xk857.simpleeffect.R;
import com.xk857.simpleeffect.domain.Result;
import com.xk857.simpleeffect.domain.entity.Project;
import com.xk857.simpleeffect.domain.param.AddProjectParam;
import com.xk857.simpleeffect.enums.RequestEnum;
import com.xk857.simpleeffect.utils.CalendarUtils;
import com.xk857.simpleeffect.utils.OkHttpUtils;
import com.xuexiang.xui.widget.actionbar.TitleBar;
import com.xuexiang.xui.widget.button.roundbutton.RoundButton;
import com.xuexiang.xui.widget.dialog.materialdialog.MaterialDialog;
import com.xuexiang.xui.widget.toast.XToast;

import java.lang.reflect.Type;
import java.util.Calendar;
import java.util.function.Consumer;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;

public class AddProjectActivity extends AppCompatActivity {

    private EditText titleEdit;
    private EditText remarkEdit;
    private LinearLayout matterDateLayout;
    private TextView dateText;
    private RoundButton addBtn;

    private AddProjectParam addProjectParam;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_project);
        initView();
        initDate();
    }

    private void initDate() {
        addProjectParam = new AddProjectParam(0);
        matterDateLayout.setOnClickListener(v -> {
            Calendar calendar = Calendar.getInstance();
            if (addProjectParam.getFinishTime()!=null) {
                calendar = CalendarUtils.getCalenderByDateTextSplit(addProjectParam.getFinishTime());
            }

            DatePickerDialog datePickerDialog = new DatePickerDialog(this, com.xuexiang.xui.R.style.XUITheme_AlertDialog, (view, year, month, day) -> {
                Calendar calendar1 = Calendar.getInstance();
                calendar1.set(year, month, day);
                addProjectParam.setFinishTime(DateUtil.format(DateUtil.date(calendar1), "yyyy-MM-dd"));
                dateText.setText(CalendarUtils.getDataStr(calendar1, "yyyy-MM-dd"));
            }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
            datePickerDialog.show();
        });

        addBtn.setOnClickListener(v -> {
            if (StrUtil.isBlank(titleEdit.getText().toString())) {
                XToast.info(this,"项目名称不可为空").show();
                return;
            }
            addProjectParam.setName(titleEdit.getText().toString());
            addProjectParam.setElaborate(remarkEdit.getText().toString());
            Type type = new TypeToken<Result<Project>>() {}.getType();
            OkHttpUtils.getInstance().post(this, RequestEnum.ADD_PROJECT, type, addProjectParam, true, (Consumer<Project>) project -> {
                if (project.getCreateTime() != null) {
                    runOnUiThread(() ->
                            new MaterialDialog.Builder(this).iconRes(R.drawable.icon_success).title(R.string.success).content("项目添加成功").positiveText(R.string.yes).onPositive((dialog, which) -> finish()).show()
                    );
                }
            });
        });
    }

    private void initView() {
        TitleBar titleBar = findViewById(R.id.title_bar);
        titleBar.setLeftClickListener(v -> finish());
        titleEdit = findViewById(R.id.title_edit);
        remarkEdit = findViewById(R.id.remark_edit);
        matterDateLayout = findViewById(R.id.matter_date_layout);
        dateText = findViewById(R.id.date_text);
        addBtn = findViewById(R.id.add_btn);
    }
}